//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios


//====axios的前置拦截器:作用，每次访问后台【都是使用axios发送请求的】，都要将token携带到后端====
axios.interceptors.request.use(res=>{
    //从浏览器中获取token
    let token = localStorage.getItem("token");
    if(token){
        //将token添加到请求头
        res.headers["token"] = token;
    }
    return res;//语法格式，必须返回
})
//======================axios的前置拦截器====================//

//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    console.log("==============1============");
    console.log(res);
    //后端响应的是没有登录的信息
    if (false === res.data.success && "noLogin" === res.data.msg) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        //跳转到登录页面
        location.href="/login.html";
    }else if(false === res.data.success && "timeOut" === res.data.msg){
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        //跳转到登录页面
        location.href="/login.html";
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//




//======================路由的前端拦截器【拦截不到后端的请求】====================//
//注释掉登陆拦截：判断session中有没有user对象并且路径不是login就调转到登录页面

let url = location.href;//http://localhost/login.html
let path = url.substring(url.lastIndexOf("/"))//   /login.html
if(path!="/login.html" && path!="/register.html" &&
    url.indexOf("/binder.html")==-1 && url.indexOf("/callback.html")==-1){
  let logininfo= localStorage.getItem("logininfo");
    if(!logininfo){
        location.href="login.html"
    }
};
//======================路由的前端拦截器【拦截不到后端的请求】====================//


function parseUrlParams2Obj(url) {//   xxx?code=111&key=22222

    var obj={}
    var params =url.substring(url.indexOf("?")+1)//  code=111&key=22222
    var paramsArr=params.split("&")//   code=111,key=22222
    for(var i=0 ;i<paramsArr.length;i++){
        var param = paramsArr[i]
      var paramArr = param.split("=")
        obj[paramArr[0]]=paramArr[1]
    }
    return obj
}